#include "playerlayout.h"
#include <math.h>


PlayerLayout::PlayerLayout()
{

}

PlayerLayout::QRectArray PlayerLayout::calcLayout(const QRect& rect, Mode mode)
{
    QRectArray rects;
    if (mode == kOne)
    {
        rects.push_back(rect);
    }
    else if (mode == kFour)
    {
        rects = calcSquare(rect, mode);
    }
    else if (mode == kNine)
    {
        rects = calcSquare(rect, mode);
    }
    else if (mode == kSixteen)
    {
        rects = calcSquare(rect, mode);
    }
    else
    {
        rects.push_back(rect);
    }

    return rects;
}

PlayerLayout::QRectArray PlayerLayout::calcSquare(const QRect& rect, Mode mode)
{
    QRectArray rects;
    int rows = (int)sqrt((int)mode);
    int cols = (int)sqrt((int)mode);

    int width = rect.width() / rows;
    int height = rect.height() / cols;

    for (int i = 0; i < mode; i ++)
    {
        int x = rect.x() + 1 + (i % rows)*width;
        int y = rect.y() + 1 + (i / cols)*height;
        QRect subRect(QPoint(x,y), QSize(width, height));
        rects.push_back(subRect);
    }

    return rects;
}
